package com.eipc;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import com.eipc.event.OnFetchOtherFinishedListener;
import com.eipc.model.Department;
import com.eipc.model.Game;
import com.eipc.model.News;
import com.eipc.model.Person;
import com.eipc.model.Weather;
import com.eipc.util.Tools;

public class Employee extends CommonActivity  implements TabContentFactory{
	private TabHost mTabHost;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.import_notice);
		initView();
		 new FetchOtherTask(this, mDB,new OnFetchOtherFinishedListener() {	
				@Override
				public void OnFinished(ArrayList<News> news, Game game, Weather weather) {
					if(news!=null){
						for(News n : news){
							mInfos.addView(getNewsView(n));
						}
					}
					mInfos.startFlipping();
					mBottomBar.setGame(game);
					mBottomBar.setWeather(weather);
				}
			}).execute();
	}
	
	private View buildLabel(int label){
    	LayoutInflater inflater = getLayoutInflater();
    	Button btn = (Button)inflater.inflate(R.layout.label, null);
    	btn.setText(label);
    	return btn;
    }
	
	@Override
	public View createTabContent(String tag) {
		if("user".equals(tag)){
			ListView lv = (ListView)mInflater.inflate(R.layout.list, null);
			lv.setAdapter(new UserAdapter(mDB.getAllPerson()));
			return lv;
		}
		else if("group".equals(tag)){
			ListView lv = (ListView)mInflater.inflate(R.layout.list, null);
			lv.setAdapter(new GroupAdapter(mDB.getAllDepartment()));
			return lv;
		}
		return null;
	}
	
	@Override
	protected void initView() {
		super.initView();
		mBar.setMiddleText(R.string.employee);
		mTabHost = (TabHost)findViewById(android.R.id.tabhost);
		mTabHost.setup();
		mTabHost.addTab(mTabHost.newTabSpec("user")
						.setIndicator(buildLabel(R.string.emp_btn_user))
						.setContent(this));
		mTabHost.addTab(mTabHost.newTabSpec("group")
				.setIndicator(buildLabel(R.string.emp_btn_group))
				.setContent(this));	
	}
	
	class UserAdapter extends BaseAdapter{
		private ArrayList<Person> persons;
		public UserAdapter(ArrayList<Person> persons){
			this.persons = persons;
		}
		@Override
		public int getCount() {
			if(persons==null || persons.size()==0) return 0;
			return persons.size()+1;
		}

		@Override
		public Object getItem(int position) {
			if(position==0) return persons;
			return persons.get(position-1);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = mInflater.inflate(R.layout.employee_user_item, null);
			CheckBox cb = (CheckBox)view.findViewById(R.id.cb_name);
			Button call = (Button)view.findViewById(R.id.btn_call);
			Button sms = (Button)view.findViewById(R.id.btn_sms);
			Button qm = (Button)view.findViewById(R.id.btn_qm);
			if(position==0){
				cb.setText(R.string.all);
			}
			else{
				final Person p = persons.get(position-1);
				cb.setText(p.name);
				call.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Tools.sendCall(Employee.this, p.mobile);
					}
				});
				sms.setOnClickListener(new OnClickListener() {	
					@Override
					public void onClick(View v) {
						Tools.sendMsg(Employee.this, p.mobile, "");
					}
				});
				//TODO gps
				qm.setOnClickListener(null);
			}
			return view;
		}
		
	}
	
	
	class GroupAdapter extends BaseAdapter{
		private ArrayList<Department> departments;
		public GroupAdapter(ArrayList<Department> departments){
			this.departments = departments;
		}
		@Override
		public int getCount() {
			if(departments==null || departments.size()==0) return 0;
			return departments.size()+1;
		}

		@Override
		public Object getItem(int position) {
			if(position==0) return departments;
			return departments.get(position-1);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = mInflater.inflate(R.layout.employee_group_item, null);
			CheckBox cb = (CheckBox)view.findViewById(R.id.cb_name);
			Button sms = (Button)view.findViewById(R.id.btn_sms);
			Button qm = (Button)view.findViewById(R.id.btn_qm);
			if(position==0){
				cb.setText(R.string.all);
			}
			else{
				final Department d = departments.get(position-1);
				cb.setText(d.name);
				sms.setOnClickListener(new OnClickListener() {	
					@Override
					public void onClick(View v) {
						Tools.sendMsg(Employee.this, "", "");
					}
				});
				//TODO gps
				qm.setOnClickListener(null);
			}
			return view;
		}
		
	}
}